//
//  ZMSelectDate.swift
//  TravelForUser
//
//  Created by Rowling on 2020/8/15.
//  Copyright © 2020 智蚂优服. All rights reserved.
//

import UIKit
import PGDatePicker

class ZMSelectDateView: XMBaseView {
    
    public class func date(config: SelectDateConfig, clickBlock: ((_ selectDate: NSDate)->())?) {
        let view = ZMUtil.loadNibView(nibName: ZMSelectDateView.self)
        view.frame = SCREEN_BOUNDS
        view.backgroundColor = config.backgroundColor
        ZMUtil.clipCircle(view: view.bgView, Corner: [.topLeft, .topRight], radius: 8.0)
        view.selectDateBlock = clickBlock
        view.cancelBtn.setTitle(config.cancelBtnText, for: .normal)
        view.cancelBtn.setTitleColor(config.cancelBtnTextColor, for: .normal)
        view.cancelBtn.titleLabel?.font = config.cancelTextFont
        view.titleLabel.text = config.title
        view.titleLabel.font = config.titleTextFont
        view.titleLabel.textColor = config.titleTextColor
        view.sureBtn.setTitle(config.sureBtnText, for: .normal)
        view.sureBtn.setTitleColor(config.sureBtnTextColor, for: .normal)
        view.sureBtn.titleLabel?.font = config.sureTextFont
        view.config = config
        view.picker.delegate = view
        APPWindow?.addSubview(view)
    }
    
    @IBOutlet weak var bgView: UIView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var cancelBtn: UIButton!
    @IBOutlet weak var sureBtn: UIButton!
    @IBOutlet weak var pickerView: UIView!
    lazy var picker: PGDatePicker = {
        let picker = PGDatePicker()
        picker.autoSelected = true
        picker.datePickerType = .line
        picker.lineBackgroundColor = config.separatorColor
        picker.isHiddenMiddleText = false
        picker.showUnit = config.unitType
        picker.maximumDate = config.maxDate as Date
        picker.textFontOfSelectedRow = config.selectedFont
        picker.textFontOfOtherRow = config.otherFont
        picker.textColorOfSelectedRow = config.selectedColor
        picker.textColorOfOtherRow = config.otherColor
        picker.rowHeight = config.cellCellHeight
        picker.datePickerMode = config.pickerMode
        picker.minimumDate = config.minDate as Date
        picker.setDate(config.defDate as Date)
        pickerView.addSubview(picker)
        picker.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        return picker
    }()
    var selectDateBlock:((_ date: NSDate)->())?
    var selectDate: DateComponents?
    var config = SelectDateConfig()
    
}

extension ZMSelectDateView: PGDatePickerDelegate {
    func datePicker(_ datePicker: PGDatePicker!, didSelectDate dateComponents: DateComponents!) {
        selectDate = dateComponents
    }
}

extension ZMSelectDateView {
    @IBAction func cancelClick(_ sender: UIButton) {
        self.removeFromSuperview()
    }
    
    @IBAction func sureClick(_ sender: UIButton) {
        guard let date = selectDate else {
            return
        }
        let dateStr: String = "\(date.year!)-\(date.month!)-\(date.day!)"
        if let block = selectDateBlock {
            block(dateStr.zm.toDate()! as NSDate)
        }
        self.removeFromSuperview()
    }
}



// MARK: - 属性配置
class SelectDateConfig {
    
    public var from = SCREEN_BOUNDS
    /// 背景色
    public var backgroundColor = UIColor.zm.color(hexString: "#000000", 0.5)
    /// 标题
    public var title = ""
    /// 标题字体颜色
    public var titleTextColor: UIColor = .black
    /// 标题字体
    public var titleTextFont: UIFont = UIFont.zm.fontRegular(size: 14)
    /// 取消按钮文字颜色，默认333333
    public var cancelBtnTextColor: UIColor = UIColor.zm.color333333()
    /// 取消按钮文字,默认取消
    public var cancelBtnText: String = "取消"
    /// 取消按钮字体
    public var cancelTextFont: UIFont = UIFont.zm.fontRegular(size: 14)
    /// 确定按钮文字颜色，默认红色
    public var sureBtnTextColor: UIColor = UIColor.zm.color(hexString: "#F3625D")
    /// 确定按钮文字，默认确定
    public var sureBtnText: String = "确定"
    /// 确定按钮字体
    public var sureTextFont: UIFont = UIFont.zm.fontSemibold(size: 14)
    /// 设置 picker 中间两条分割线的背景颜色
    public var separatorColor: UIColor = .clear
    /// cell高度
    public var cellCellHeight: CGFloat = 30.0
    /// 日期选择器显示类型
    public var pickerMode: PGDatePickerMode = .date
    /// 最小时间，默认1100-01-01
    public var minDate: NSDate = "1100-01-01".zm.toDate()! as NSDate
    /// 最大时间，默认现在
    public var maxDate: NSDate = NSDate()
    /// 默认时间
    public var defDate: NSDate = NSDate()
    /// 单位
    public var unitType: PGShowUnitType = .none
    /// 已选择字体
    public var selectedFont = UIFont.zm.fontSemibold(size: 14)
    /// 默认字体
    public var otherFont = UIFont.zm.fontRegular(size: 14)
    /// 已选择字体颜色
    public var selectedColor = UIColor.zm.color333333()
    /// 默认字体颜色
    public var otherColor = UIColor.zm.color999999()
    
}
